package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.bo.goods.GoodsCartBO;
import com.ymk.bo.goods.GoodsSkuBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.enums.goods.GoodsTypeEnum;
import com.ymk.service.IGoodsCartService;
import com.ymk.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class GoodsCartPostCmd extends ACmd {

    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsCartBO reqData = super.getData(sign, GoodsCartBO.class);

        //设置一个map、有商品id、还有商品的skuid、以及用户id
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("goodsId",reqData.getGoodsId())
                .addParam("skuId",reqData.getSkuId())
                .addParam("userId",sign.getUserId())
                .addParam("id",reqData.getUserId());
      //先查询购物车中是否存在此商品，如果存在则在数量上加入此次添加商品即可
        GoodsCartBO dbGoodsCartBO = this.goodsCartService.get(paramsBuilder.build());
        if (Objects.nonNull(dbGoodsCartBO)){
            GoodsCartBO editGoodsCart = new GoodsCartBO();
            editGoodsCart.setId(dbGoodsCartBO.getId());
            editGoodsCart.setGoodsCount(dbGoodsCartBO.getGoodsCount() + reqData.getGoodsCount());
            super.setModifiedData(sign,editGoodsCart);

            this.goodsCartService.update(editGoodsCart);
            return respSign;
        }
//如果购物车中不存在，则先清空map
        paramsBuilder.clearParam()
                .addParam("id", reqData.getGoodsId())
                .addParam("loadSku",true);//
        GoodsBO goodsBO = this.goodsService.get(paramsBuilder.build());

        GoodsCartBO goodsCartBO = new GoodsCartBO();
        goodsCartBO.setMchId(sign.getMchId());
        goodsCartBO.setUserId(sign.getUserId());
        goodsCartBO.setGoodsId(goodsBO.getId());

        Map<Integer, GoodsSkuBO> goodsSkuBOMap = goodsBO.getGoodsSkuBOList().stream().collect(Collectors.toMap(GoodsSku::getId, x -> x));
        GoodsSkuBO goodsSkuBO = goodsSkuBOMap.get(reqData.getSkuId());
        goodsCartBO.setSkuId(goodsSkuBO.getId());

        if (GoodsTypeEnum.SIMPLE.getType().equals(goodsBO.getType())){
            goodsCartBO.setSkuName(goodsBO.getTitle());
        }else{
            goodsCartBO.setSkuName(goodsBO.getTitle() + goodsSkuBO.getPropName());
        }

        goodsCartBO.setGoodsCount(reqData.getGoodsCount());
        goodsCartBO.setRemark(reqData.getRemark());
        super.setCreatedData(sign,goodsCartBO);

        this.goodsCartService.post(goodsCartBO);

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        GoodsCartBO goodsCartBO = super.getData(sign, GoodsCartBO.class);
        if (Objects.isNull(goodsCartBO.getGoodsId())){
            throw new MallException("商品id为空");
        }
        return null;
    }

    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_CART_POST;
    }

    @Autowired
    private IGoodsCartService goodsCartService;
    @Autowired
    private IGoodsService goodsService;
}